package com.ay.smartpark.sdk.common.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.qycloud.SDK.server.dto.DataCenterDataParamDTO;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static java.util.Arrays.asList;

/**
 * @program: sdkclient
 * @description: 条件组装工具类
 * @author: ChengQi
 * @create: 2019-06-24 13:49
 **/
public class ConditionUtils {

    Log logger = LogFactory.getLog(this.getClass());


    /** 
    * @Description:
    * @Param: [paramsMap, tableId] 
    * @return: com.qycloud.SDK.server.dto.DataCenterDataParamDTO 
    * @Author: ChengQi 
    * @Date: 2019/7/5 
    */ 
    public static DataCenterDataParamDTO fitDataCenterDataParam(Map<String, Object> paramsMap, String tableId,
                                                                String userId, String entId) {
        DataCenterDataParamDTO dataCenterDataParamDTO = new DataCenterDataParamDTO();
        dataCenterDataParamDTO.setEntId(entId);
        dataCenterDataParamDTO.setUserId(userId);
        dataCenterDataParamDTO.setTableId(tableId);
        dataCenterDataParamDTO.setPageCondition(fitPageCondition(paramsMap));
        dataCenterDataParamDTO.setSortCondition(fitSortCondition(paramsMap));
        dataCenterDataParamDTO.setSearchFields(fitSearchFields(paramsMap));
        dataCenterDataParamDTO.setConditions(fitConditions(paramsMap));
        return dataCenterDataParamDTO;
    }

    /**
    * @Description: 组装查询条件
    * @Param: [paramsMap]
    * @return: java.util.List<java.util.Map<java.lang.String,java.lang.String>>
    * @Author: ChengQi
    * @Date: 2019/7/5
    */
    private static List<Map<String, String>> fitConditions(Map<String, Object> paramsMap) {
        List<Map<String, String>> conditions = new ArrayList<Map<String, String>>();
        if (paramsMap.containsKey("conditions") && StringUtils.isNotBlank(paramsMap.get("conditions").toString())) {
            JSONArray jsonArray = JSON.parseArray(paramsMap.get("conditions").toString());
            for (int i = 0; i < jsonArray.size(); i++) {
                conditions.add((Map) jsonArray.get(i));
            }
            return conditions;
        }
        return conditions;

    }

    /**
    * @Description: 组装查询字段
    * @Param: [paramsMap]
    * @return: java.util.List<java.lang.String>
    * @Author: ChengQi
    * @Date: 2019/7/5
    */
    private static List<String> fitSearchFields(Map<String, Object> paramsMap) {
        if(paramsMap.containsKey("searchFields") && StringUtils.isNotBlank(paramsMap.get("searchFields").toString())){

            String fieldsStr = paramsMap.get("searchFields").toString();
            List<Map<String, String>> list = new ArrayList<>();
            return asList(paramsMap.get("searchFields").toString().split(","));
        }

        return new ArrayList<String>();
    }

    /**
    * @Description:  组装排序条件
    * @Param: [paramsMap]
    * @return: java.util.Map<java.lang.String,java.lang.String>
    * @Author: ChengQi
    * @Date: 2019/7/5
    */
    private static Map<String, String> fitSortCondition(Map<String, Object> paramsMap) {
        Map<String, String> map = new HashMap<String, String>(2);

        if(paramsMap.containsKey("sortCondition") && StringUtils.isNotBlank(paramsMap.get("sortCondition").toString())){

            String jsonStr =  paramsMap.get("sortCondition").toString();
            map = (Map<String, String>) JSON.parse(jsonStr);

        }

        return map;
    }

    /** 
    * @Description: 组装分页参数
    * @Param: [paramsMap] 
    * @return: void 
    * @Author: ChengQi 
    * @Date: 2019/7/5 
    */
    private static Map<String, String> fitPageCondition(Map<String, Object> paramsMap) {
        Map<String, String> pageCondition = new HashMap<String, String>(2);
        if(paramsMap.containsKey("start") && StringUtils.isNotBlank(paramsMap.get("start").toString())){
            pageCondition.put("start",paramsMap.get("start").toString());
        }
        if(paramsMap.containsKey("perPage") && StringUtils.isNotBlank(paramsMap.get("perPage").toString())){
            pageCondition.put("perPage",paramsMap.get("perPage").toString());
        }

        return pageCondition;

    }
}
